{*******************************************************************************
* The contents of this file are used with permission, subject to the Mozilla   *
* Public License Version 1.1 (the "License"); you may not use this file except *
* in compliance with the License. You may obtain a copy of the License at      *
* http://www.mozilla.org/MPL/                                                  *
*                                                                              *
* Software distributed under the License is distributed on an "AS IS" basis,   *
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for *
* the specific language governing rights and limitations under the License.    *
*                                                                              *
* The Original Code is distributed as part of the "mediate" product and is     *
* Copyright (C) @home media limited. All Rights Reserved.                      *
*                                                                              *
* Email: support@athomemedia.co.uk                                             *
* Web:   http://www.athomemedia.co.uk                                          *
*******************************************************************************}

unit MediaFolders;

interface

uses Classes;

type
  TAHMMediaFolderType = (ftMusic, ftVideo, ftImage, ftUnknown);

  TAHMMediaFolder = class
  private
    FFolderName: WideString;
    FFolderPath: WideString;
    FPersistFolder: Boolean;
    FVirtualFolder: Boolean;
  protected
  public
    property FolderName: WideString read FFolderName write FFolderName;
    property FolderPath: WideString read FFolderPath write FFolderPath;
    property PersistFolder: Boolean read FPersistFolder write FPersistFolder;
    property VirtualFolder: Boolean read FVirtualFolder write FVirtualFolder;
    function IsThisFolder(var Folder: WideString): Boolean;
    procedure SaveAsXML(Lines: TStrings);
  end;

  TAHMMediaFolderList = class
  private
    FList: TList;
  protected
    function GetCount: Integer;
    function GetFolder(Index: Integer): TAHMMediaFolder;
  public
    constructor Create;
    destructor Destroy; override;
    property Count: Integer read GetCount;
    property Folders[Index: Integer]: TAHMMediaFolder read GetFolder; default;
    procedure AddFolder(Folder: TAHMMediaFolder); overload;
    procedure AddFolder(FolderName, FolderPath: WideString; VirtualFolder: Boolean; PersistFolder: Boolean = True); overload;
    procedure DeleteFolder(Folder: TAHMMediaFolder);
    procedure Clear;
    procedure SaveAsXML(Lines: TStrings);
  end;

implementation

uses StrUtils, TagNames, XMLTags;

//----------------------------------------------------------------------------//

function TAHMMediaFolder.IsThisFolder(var Folder: WideString): Boolean;
begin
  // First check whether folder begins with our folder name
  if Pos(FFolderName, Folder) = 1 then
  begin
    // Remove virtual prefix from folder name
    Folder := RightStr(Folder, Length(Folder) - Length(FFolderName));
    Result := True;
  end
  else
    Result := False;
end;

procedure TAHMMediaFolder.SaveAsXML(Lines: TStrings);
var
  sVirt: String;
begin
  // Only include virtual attribute if this is a virtual directory
  if FVirtualFolder then sVirt := SPC+ATTR_VIRTUAL+ATB+ATTR_VAL_YES+ATQ
                    else sVirt := '';

  Lines.Add(TABS[3] + TGB+TAG_MEDIA_DIRECTORY+SPC+ATTR_NAME+ATB + EncodeXML(FFolderName) +
            ATQ + sVirt + TGC + EncodeXML(FFolderPath) + EndTag(TAG_MEDIA_DIRECTORY));
end;

//----------------------------------------------------------------------------//

constructor TAHMMediaFolderList.Create;
begin
  inherited Create;

  FList := TList.Create;
end;

destructor TAHMMediaFolderList.Destroy;
begin
  Clear;
  FList.Free;

  inherited Destroy;
end;

function TAHMMediaFolderList.GetCount: Integer;
begin
  Result := FList.Count;
end;

function TAHMMediaFolderList.GetFolder(Index: Integer): TAHMMediaFolder;
begin
  if (Index >= 0) and (Index < FList.Count) then
    Result := TAHMMediaFolder(FList.Items[Index])
  else
    Result := nil;
end;

procedure TAHMMediaFolderList.AddFolder(Folder: TAHMMediaFolder);
begin
  FList.Add(Folder);
end;

procedure TAHMMediaFolderList.AddFolder(FolderName, FolderPath: WideString; VirtualFolder: Boolean; PersistFolder: Boolean = True);
var
  Folder: TAHMMediaFolder;
begin
  Folder := TAHMMediaFolder.Create;
  Folder.FolderName := FolderName;
  Folder.FolderPath := FolderPath;
  Folder.PersistFolder := PersistFolder;
  Folder.VirtualFolder := VirtualFolder;
  AddFolder(Folder);
end;

procedure TAHMMediaFolderList.DeleteFolder(Folder: TAHMMediaFolder);
var
  i: Integer;
begin
  // Locate and free specified folder object
  i := FList.IndexOf(Folder);
  if i >= 0 then
  begin
    TAHMMediaFolder(FList[i]).Free;
    FList.Delete(i);
  end;
end;

procedure TAHMMediaFolderList.Clear;
var
  i: Integer;
begin
  for i := 0 to Pred(FList.Count) do
    TAHMMediaFolder(FList.Items[i]).Free;
  FList.Clear;
end;

procedure TAHMMediaFolderList.SaveAsXML(Lines: TStrings);
var
  i: Integer;
begin
  for i := 0 to Pred(FList.Count) do
    with TAHMMediaFolder(FList[i]) do
      if PersistFolder then SaveAsXML(Lines);
end;


end.
